package cn.raven.org.controller;

import cn.raven.basic.annotation.PreAuthorize;
import cn.raven.basic.dto.VerifyRegisterDto;
import cn.raven.basic.exception.BusinessException;
import cn.raven.basic.utils.AjaxResult;
import cn.raven.basic.utils.ExcelUtil;
import cn.raven.basic.utils.PageList;
import cn.raven.org.domain.Shop;
import cn.raven.org.domain.ShopAuditLog;
import cn.raven.org.query.ShopQuery;
import cn.raven.org.service.IShopService;
import cn.raven.org.vo.ShopVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**

* @Description: 店铺接口类
* @Author: king
* @Version: 1.0
* @Date:  2022/6/24 10:10  
  */
@RestController
@RequestMapping("/shop")
@Api(value = "店铺的API",description="店铺相关的CRUD功能")
public class ShopController {
    @Autowired
    private IShopService shopService;

    /**
    * @Title: findAll
    * @Description: 查询店铺所有
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 9:56
    * @Parameters: []
    * @Return java.util.List<cn.raven.org.domain.Shop>
      */
    @GetMapping
    private List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
    * @Title: findById
    * @Description: 根据id查询数据
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 9:57
    * @Parameters: [id]
    * @Return cn.raven.org.domain.Shop
      */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "id查询店铺",sn = "shop:id")
    public Shop findById(@ApiParam(value="根据id查询",required = true) @PathVariable("id") Long id){
        return shopService.findOne(id);
    }

    /**
    * @Title: del
    * @Description: 根据id删除数据
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 9:59
    * @Parameters: [id]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除店铺",sn = "shop:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        shopService.del(id);
        return AjaxResult.me();
    }
    
    /** 
    * @Title: save
    * @Description: 新增/修改
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 10:01  
    * @Parameters: [shop]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PutMapping
    @PreAuthorize(name = "保存店铺",sn = "shop:save")
    public AjaxResult save(@RequestBody Shop shop){
        try {
            if (shop.getId() == null){
                shopService.add(shop);
            }else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
    
    /** 
    * @Title: queryDate
    * @Description: 分页+高级查询
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 10:34  
    * @Parameters: [shopQuery]
    * @Return cn.raven.basic.utils.PageList<cn.raven.org.domain.Shop>
      */
    @PostMapping
    @PreAuthorize(name = "店铺列表",sn = "shop:list")
    public PageList<Shop> queryDate(@RequestBody ShopQuery shopQuery){
        return shopService.queryData(shopQuery);
    }
    
    /** 
    * @Title: batchDel
    * @Description: 根据ids 批量删除
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/25 12:29
    * @Parameters: [ids]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PatchMapping
    @PreAuthorize(name = "批量删除店铺",sn = "shop:batchDelete")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            shopService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
    
    /** 
    * @Title: settlement
    * @Description: 店铺入驻
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/1 16:03
    * @Parameters: [shop]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误");
        }
    }
    /**
    * @Title: pass
    * @Description: 店铺入驻审核通过
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/2 17:59
    * @Parameters: [shopAuditLog]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/audit/pass")
    @PreAuthorize(name = "店铺入驻审核通过",sn = "shop:audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog, HttpServletRequest request){
        try {
            shopService.pass(shopAuditLog,request);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误");
        }
    }
    /**
    * @Title: reject
    * @Description: 店铺入驻审核驳回
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/2 17:59
    * @Parameters: [shopAuditLog]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/audit/reject")
    @PreAuthorize(name = "店铺入驻审核驳回",sn = "shop:audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog,HttpServletRequest request){
        try {
            shopService.reject(shopAuditLog,request);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误");
        }
    }
    
    /** 
    * @Title: not
    * @Description: 店铺入驻审核拒接
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/3 11:20
    * @Parameters: [shopAuditLog]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/audit/not")
    @PreAuthorize(name = "店铺入驻审核拒绝",sn = "shop:audit/not")
    public AjaxResult not(@RequestBody ShopAuditLog shopAuditLog,HttpServletRequest request){
        try {
            shopService.not(shopAuditLog,request);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误");
        }
    }

    /**
    * @Title: live
    * @Description: 点击激活邮件，跳转接口修改状态为已审核
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/3 9:05
    * @Parameters: [id]
    * @Return void
      */
    @GetMapping("/live/{id}")
    public void live(@PathVariable("id") Integer id){
        shopService.live(id);
    }
    /**
    * @Title: export
    * @Description: 导出
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/3 9:05
    * @Parameters: [response]
    * @Return void
      */
    @GetMapping("/excel/export")
    @PreAuthorize(name = "excel导出",sn = "shop:export")
    public void excelExport(HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        //List<Map<String, Object>> list, String fileName, HttpServletResponse response
        ExcelUtil.exportExcel(shops,null,"店铺入驻",Shop.class,"宠物乐园.xlsx",response);
    }

    /**
    * @Title: export
    * @Description: 导入
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/3 9:06
    * @Parameters: [response]
    * @Return void
      */
    @PostMapping("/excel/import")
    @PreAuthorize(name = "excel导入",sn = "shop:import")
    public void excelImport(@RequestPart(value = "file")MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shopService.batchAdd(shops);
    }
    /**
    * @Title: echarts
    * @Description: 首页柱状图
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/7 15:05
    * @Parameters: [shopVo]
    * @Return java.util.Map<java.lang.String,java.lang.Object>
      */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(ShopVo shopVo){
        return shopService.echarts(shopVo);
    }
    /**
    * @Title: smsCode
    * @Description: 发送手机号验证
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/7 15:06
    * @Parameters: [shop]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody Shop shop){
        try {
            shopService.smsCode(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,骚后重试");
        }
    }
}
